home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Snippets / PNL Libraries / MyPrinting.p < prev    next >
Text File  |  1997-03-25  |  7KB  |  243 lines

  1. unit MyPrinting;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Printing;
  7.  
  8.     type
  9.         PObject = object
  10.                 procedure Create;
  11.                 procedure Destroy;
  12.                 function CountPages (r: Rect): integer;
  13.                 function DrawPage (r: Rect; gp: GrafPtr; pg: integer; first, last: boolean): OSErr;
  14.                 procedure OpenPrintingStatusDialog;
  15.                 procedure DoIdle;
  16.                 procedure ClosePrintingStatusDialog;
  17.             end;
  18.  
  19.     var
  20.         thePrintingRecordHandle: THPrint;
  21.  
  22.     procedure StartupPrinting;
  23.     function PrintStuff (pob: PObject; thePrRecHdl: THPrint): OSErr; { may return userCanceledErr }
  24.     function DoPageSetup (pob: PObject; thePrRecHdl: THPrint): OSErr;
  25.  
  26. implementation
  27.  
  28.     uses
  29.         Quickdraw, ToolUtils, Resources, 
  30.         MyCursors, MyStartup;
  31.  
  32.     procedure PObject.Create;
  33.     begin
  34.     end;
  35.  
  36.     procedure PObject.Destroy;
  37.     begin
  38.         dispose(self);
  39.     end;
  40.  
  41.     function PObject.CountPages (r: Rect): integer;
  42.     begin
  43. {$unused( r )}
  44.         CountPages := 1;
  45.     end;
  46.  
  47.     procedure PObject.DoIdle;
  48.     begin
  49.     end;
  50.  
  51.     function PObject.DrawPage (r: Rect; gp: GrafPtr; pg: integer; first, last: boolean): OSErr;
  52.     begin
  53. {$unused( pg, first, last )}
  54.         SetPort(gp);
  55.         with r do
  56.             MoveTo((left + right) div 2 - 20, (top + bottom) div 2);
  57.         DrawString('Not Yet Implemented');
  58.         DrawPage := noErr;
  59.     end;
  60.  
  61.     procedure PObject.OpenPrintingStatusDialog;
  62.     begin
  63.         CursorSetWatch;
  64.     end;
  65.  
  66.     procedure PObject.ClosePrintingStatusDialog;
  67.     begin
  68.         CursorSetArrow;
  69.     end;
  70.  
  71.     var
  72.         gpob: PObject;
  73.  
  74.     procedure DoIdle;
  75.     begin
  76.         gpob.DoIdle;
  77.     end;
  78.  
  79.     function DoPageSetup (pob: PObject; thePrRecHdl: THPrint): OSErr;
  80.         var
  81.             dummy: boolean;
  82.     begin
  83. {$unused( pob )}
  84.         PrOpen;
  85.         if PrError = noErr then begin
  86.             dummy := PrStlDialog(thePrRecHdl);
  87.             DoPageSetup := noErr;
  88.         end
  89.         else begin
  90.             DoPageSetup := PrError;
  91.         end;
  92.         PrClose;
  93.     end;
  94.  
  95. {*------ PrintStuff ---------------------------------------------------------*}
  96. {** **   PrintStuff will call all of the necessary Print Manager calls to print }
  97. {**   a document.  It checks PrError() after each Print Manager call.  If an }
  98. { **   error is found, all of the Print Manager open calls (i.e., PrOpen, }
  99. { **   PrOpenDoc...) will have a corresponding close call before the error }
  100. { **   is posted to the user.  You want to use this approach to make sure the }
  101. { **   Print Manager closes properly and all temporary memory is released. }
  102.     function PrintStuff (pob: PObject; thePrRecHdl: THPrint): OSErr;
  103.  
  104.         var
  105.             copies, firstPage, lastPage, numberOfCopies, pageNumber, printmgrsResFile, realNumberOfPagesInDoc: Integer;
  106.             oldPort: GrafPtr;
  107.             thePrPort: TPPrPort;
  108.             theStatus: TPrStatus;
  109.             err: OSErr;
  110.     begin
  111.         GetPort(oldPort);
  112.         gpob := pob;
  113.  
  114.         PrOpen;
  115.         if PrError = noErr then begin
  116.              { Save the current resource file (i.e. the printer driver's) so the driver will not lose its }
  117.             { resources upon return from the pIdleProc.}
  118.             printmgrsResFile := CurResFile;
  119.  
  120.             realNumberOfPagesInDoc := pob.CountPages(thePrRecHdl^^.prInfo.rPage);
  121.  
  122.             if PrJobDialog(thePrRecHdl) then begin
  123.  {                          Get the number of copies of the document that}
  124.  {                          the user wants printed from iCopies of the TPrJob}
  125.   {                         record (IM II-151).}
  126.  
  127.                 numberOfCopies := thePrRecHdl^^.prJob.iCopies;
  128.  
  129.   {                           Get the first and last pages of the document that}
  130.   {                          were requested to be printed by the user from}
  131.    {                         iFstPage and iLastPage from the TPrJob record}
  132.    {                         (IM II-151).}
  133.  
  134.                 firstPage := thePrRecHdl^^.prJob.iFstPage;
  135.                 lastPage := thePrRecHdl^^.prJob.iLstPage;
  136.  
  137. {                             Print "all" pages in the print loop}
  138.  
  139.                 thePrRecHdl^^.prJob.iFstPage := 1;
  140.                 thePrRecHdl^^.prJob.iLstPage := 9999;
  141.                 if (lastPage > realNumberOfPagesInDoc) then begin
  142.                     lastPage := realNumberOfPagesInDoc;
  143.                 end;
  144.  
  145.   {                           Print the number of copies of the document}
  146.  {                           requested by the user from the Print Job Dialog.}
  147.                 pob.OpenPrintingStatusDialog;
  148.  
  149.                 for copies := 1 to numberOfCopies do begin
  150.  {                               Install and call your "Print Status Dialog".}
  151.                     thePrRecHdl^^.prJob.pIdleProc := @DoIdle;
  152.  
  153.                     UseResFile(printmgrsResFile);
  154.  
  155.                     thePrPort := PrOpenDoc(thePrRecHdl, nil, nil);
  156.  
  157.                     if (PrError = noErr) then begin
  158.                             {  Print the range of pages of the document requested by the user from the Print Job Dialog.}
  159.  
  160.                         pageNumber := firstPage;
  161.                         while ((pageNumber <= lastPage) and (PrError = noErr)) do begin
  162.  
  163.                             PrOpenPage(thePrPort, nil);
  164.  
  165.                             if (PrError = noErr) then begin
  166.                                 { rPage (IM II-150) is the printable area for the currently selected printer. By passing the current}
  167.                               { enables your app to use the same routine to draw to the screen and the printer's GrafPort.}
  168.  
  169.                                 err := pob.DrawPage(thePrRecHdl^^.prInfo.rPage, GrafPtr(thePrPort), pageNumber, firstPage = pageNumber, lastPage = pageNumber);
  170.                                 if err <> noErr then begin
  171.                                     PrSetError(err);
  172.                                 end;
  173.                             end;
  174.                             PrClosePage(thePrPort);
  175.                             pageNumber := pageNumber + 1;
  176.                         end;  {**  End pagenumber loop  **}
  177.                     end;
  178.                     PrCloseDoc(thePrPort);
  179.                 end;  {**  End copies loop  **}
  180.  
  181.                 pob.ClosePrintingStatusDialog;
  182.   {                            The printing job is being canceled by the request}
  183.   {                            of the user from the Print Style Dialog or the}
  184.   {                            Print Job Dialog PrError will be set to iPrAbort}
  185.    {                           to tell the Print Manager to abort the current}
  186.    {                           printing job.}
  187.             end
  188.             else begin
  189.                 PrSetError(iPrAbort); {** Cancel from the job dialog **}
  190.             end;
  191.         end;
  192.         if (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) then begin
  193.             PrPicFile(thePrRecHdl, nil, nil, nil, theStatus);
  194.         end;
  195.  
  196.   {        Grab the printing error before you close}
  197.  {        the Print Manager and the error disappears.}
  198.  
  199.         if PrError = iPrAbort then begin
  200.             PrintStuff := userCanceledErr;
  201.         end
  202.         else begin
  203.             PrintStuff := PrError;
  204.         end;
  205.  
  206.         PrClose;
  207.  
  208.         SetPort(oldPort);
  209.     end;  {**  PrintStuff  **}
  210.  
  211.     function InitPrinting(var msg: integer): OSStatus;
  212.     begin
  213. {$unused(msg)}
  214.         thePrintingRecordHandle := THPrint(NewHandle(SIZEOF(TPrint)));
  215.         PrOpen;
  216.         if PrError = noErr then begin
  217.             PrintDefault(thePrintingRecordHandle);
  218.             PrClose;
  219.         end;
  220.         InitPrinting := noErr;
  221.     end;
  222.  
  223.     procedure FinishPrinting;
  224.     begin
  225.         DisposeHandle(Handle(thePrintingRecordHandle));
  226.     end;
  227.  
  228.     procedure StartupPrinting;
  229.     begin
  230.         StartupCursors;
  231.         SetStartup(InitPrinting, nil, 0, FinishPrinting);
  232.     end;
  233.     
  234. end.
  235. procedure PObject.PostPrintingErrors (oe: OSErr);
  236.     var
  237.         s: Str255;
  238.         a: integer;
  239. begin
  240.     NumToString(oe, s);
  241.     ParamText('Print Error = ', s, '', '');
  242.     a := Alert(fail_alert_id, nil);
  243. end;